package app.android.kdictionary.Util;



import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;



public class HttpURLMgr {
	private String[][] jasoChkList = {{"ㄱ","ㄴ","ㄷ","ㄹ","ㅁ","ㅂ","ㅅ","ㅇ","ㅈ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ","ㄲ","ㄸ","ㅃ","ㅆ","ㅉ","?"},
			{"ㅏ","ㅑ","ㅓ","ㅕ","ㅗ","ㅛ","ㅜ","ㅠ","ㅡ","ㅣ","ㅐ","ㅒ","ㅔ","ㅖ","ㅘ","ㅙ","ㅚ","ㅝ","ㅞ","ㅟ","ㅢ","?"},
			{"ㄱ","ㄴ","ㄷ","ㄹ","ㅁ","ㅂ","ㅅ","ㅇ","ㅈ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ","ㄲ","ㅆ","ㄳ","ㄵ","ㄶ","ㄺ","ㄻ","ㄼ","ㄽ","ㄾ","ㄿ","ㅀ","ㅄ","?","-"}};
	
	private String baseUrl = "http://124.137.201.223/"; 
	private String viewUrl = "search/View.jsp";
	private String[] targetUrl = {"search/List_dic.jsp"		
								 ,"section/idiom_list.jsp"	, "section/proverb_list.jsp"
								 ,"section/region_list.jsp"	, "section/north_list.jsp"	, "section/origin_list.jsp"};
	
	private int type;
	private int section;
	private String[] sectionTable = {"", "PROVERB", "PROVERB", "WORD", "WORD", "WORD"};
	private int page = 1;
	private HttpURLParamsBean params;
	
	public HttpURLMgr() {
		params = new HttpURLParamsBean();
		params.setPageRow("20");
		params.setGo("1");
		
	}
	
	public String getViewURL(String idx) {
		return baseUrl + viewUrl + "?idx=" + idx;
	}
	
	public String getSimpleSearchURL() {
		
		return baseUrl + targetUrl[0];
	}
	
	public String getMultiSearchURL() {
		
		return baseUrl + targetUrl[0];
	}
	
	public String getSectionSearchURL(int section) {
		
		return baseUrl + targetUrl[section];
	}
	
	public void setSimpleSearchParams(String searchText) {
		//Simple, SearchText, go, gogroup, PageRow=20, SearchPart=Simple, Table=words|word, Gubun=0
		type = 0;
		params.setSearchText(new String[]{searchText});
		params.setTable(new String[]{"words|word"});
		params.setGubun(new String[]{"0"});
		params.setSearchPart("Simple");
	}
	
	public void setMultiSearchParams(String[] searchText, String[] table, String[] gubun, String[] orgLang, String[] techTerm, String[] spCode) {
		//Multi, 
		type = 1;
		if(searchText.length == 1) {
			params.setSearchPart("Simple");
		} else {
			params.setSearchPart("Multi");
		}
		params.setOrgLang(orgLang);
		params.setTechTerm(techTerm);
		params.setSearchText(searchText);
		params.setTable(table);
		params.setGubun(gubun);
		params.setSpCode(spCode);
		params.setArrSearchLen(String.valueOf(searchText.length-1));
		params.setSetJaso("");
		params.setResultRow("0");
		params.setSearchStartText("");
		params.setSearchEndText("");
		params.setJasoCnt("");
		params.setJasoSearch("");
	}
	
	public void setJamoSearchParams(String jamo) {
		//Index
		type = 2;
		params.setSearchPart("Index");
		params.setSetJaso(jamo);
		params.setArrSearchLen("0");
		params.setResultRow("0");
		params.setGubun(new String[]{"0"});
		params.setSearchText(new String[]{""});
		params.setSearchStartText("");
		params.setSearchEndText("");
		params.setJasoCnt("0");
		params.setJasoSearch("");
		params.setTable(new String[]{"words|word"});
		params.setImeMode("");
	}

	public void setRangeSearchParams(String startText, String endText) {
		//Range
		type = 3;
		params.setSearchPart("Range");
		params.setSearchStartText(startText);
		params.setSearchEndText(endText);
		params.setTable(new String[]{"words|word"});
	}

	public void setJasoSearchParams(String[] jaso) throws Exception {
		//Jaso
		String jasoString = "";
		int jasoCnt = 0;
		int jasoSize = jaso.length;
		boolean jasoChk = true;
		for(int i = 0 ; i < 4 && jasoChk; i++) {
			jasoString += "[";
			for(int j = 0 ; j < 3 ; j++) {
				if(jasoSize < i*3+j) {
					jasoString += "?";
					jasoChk = false;
				} else if(jaso[i*3+j].trim().length() == 0) {
					jasoString += "?";
				} else {
					String tmpJaso = jaso[i*3+j].trim();
					boolean jasoValidation = false;
					for(int k = 0 ; k < jasoChkList[j].length ; k++) {
						if(jasoChkList[j][k].equals(tmpJaso)) {
							jasoValidation = true;
						}
					}
					if(jasoValidation == false) {
						//에러
						throw new InvalidArgumentException();
					} else {
						jasoString += tmpJaso;
					}
				}
				if(j != 2) {
					jasoString +="/";
				}
			}
			jasoString += "]";
			jasoCnt++;
		}
		type = 4;
		params.setJasoSearch(jasoString);
		params.setJasoCnt(jasoCnt+"");
		params.setTable(new String[]{"words|word"});
		params.setArrSearchLen("0");
		params.setSearchPart("Jaso");
	}

	public void setSectionSearchParams(String searchText, String gubun, int section) {
		//Section
		type = 5;
		this.section = section;
		params.setSearchText(new String[]{searchText});
		params.setTable(new String[]{sectionTable[section]});
		params.setGubun(new String[]{gubun});
	}
	
	public void setSectionSearchParams(String searchText, String gubun, String letter, int section) {
		//Section
		type = 5;
		this.section = section;
		params.setSearchText(new String[]{searchText});
		params.setTable(new String[]{sectionTable[section]});
		params.setGubun(new String[]{gubun});
		params.setLetter(letter);

		
	}
	
	public void setPage(int page) {
		params.setGo(String.valueOf(page));
	}
	
	public void setNextPage() {
		this.page++;
		params.setGo(String.valueOf(page));
	}

	public String getUrl() {
		switch(type) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
			return baseUrl + targetUrl[0];
		case 5:
			return baseUrl + targetUrl[section];
		case 6:
			break;
		default :
		}
		
		return "";
	}
	
	public String getParams() throws UnsupportedEncodingException{
		//
		switch(type) {
		case 0: //표제어 검색
			return "PageRow="+params.getPageRow()+"&SearchPart="+params.getSearchPart()+"&SearchText="+URLEncoder.encode(params.getSearchText()[0], "UTF-8")+"&Table="+params.getTable()[0]+"&Gubun="+params.getGubun()[0]+"&go="+params.getGo()+"&gogroup="+params.getGogroup();
		case 1: //자세히 검색
			String searchTextParam = "";
			String[] searchTextTmp = params.getSearchText(); 
			for(int i = 0; i < searchTextTmp.length ; i++) {
				searchTextParam += "&SearchText="+URLEncoder.encode(searchTextTmp[i], "UTF-8");
			}
			
			String gubunParam = "";
			String[] gubunTmp = params.getGubun();
			for(int i = 0 ; i < gubunTmp.length ; i++) {
				gubunParam += "&Gubun="+gubunTmp[i];
			}
			
			String tableParam = "";
			String[] tableTmp = params.getTable();
			for(int i = 0 ; i < tableTmp.length ; i++) {
				tableParam += "&Table="+tableTmp[i];
			}
			
			String spCode = "";
			String[] spCodeTmp = params.getSpCode();
			for(int i = 0 ; i < spCodeTmp.length ; i++) {
				if(spCodeTmp[i].trim().length() != 0)
					spCode += "&SpCode="+spCodeTmp[i];
			}
			
			String techTermParam = "";
			String[] techTermTmp = params.getTechTerm();
			for(int i = 0 ; i < techTermTmp.length ; i++) {
				techTermParam += "&TechTerm="+techTermTmp[i];
			}
			
			String orgLangParam = "";
			String[] orgLangTmp = params.getOrgLang();
			for(int i = 0 ; i < orgLangTmp.length ; i++) {
				orgLangParam += "&OrgLang="+orgLangTmp[i];
			}
			
			return "PageRow="+params.getPageRow()+"&SearchPart="+params.getSearchPart()
			+searchTextParam+gubunParam+tableParam+spCode+techTermParam+orgLangParam
			+"&SearchStartText="+params.getSearchStartText()+"&SearchEndText="+params.getSearchEndText()
			+"&ResultRows="+params.getResultRow()
			+"&JasoCnt="+params.getJasoCnt()+"&JasoSearch="+params.getJasoSearch()+"&setJaso="+params.getSetJaso()
			+"&arrSearchLen="+params.getArrSearchLen()+"&go="+params.getGo()+"&gogroup="+params.getGogroup()+"&idx="+params.getIdx()+"&ImeMode="+params.getImeMode();		
		case 2: //자모로 검색
			//
			return "PageRow="+params.getPageRow()+"&SearchPart="+params.getSearchPart()+"&SearchText="+URLEncoder.encode(params.getSearchText()[0], "UTF-8")+"&SearchStartText="+params.getSearchStartText()+"&SearchEndText="+params.getSearchEndText()
			+"&Table="+params.getTable()[0]+"&Gubun="+params.getGubun()[0]+"&ResultRows="+params.getResultRow()
			+"&JasoCnt="+params.getJasoCnt()+"&JasoSearch="+params.getJasoSearch()+"&setJaso="+params.getSetJaso()
			+"&arrSearchLen="+params.getArrSearchLen()+"&go="+params.getGo()+"&gogroup="+params.getGogroup()+"&idx="+params.getIdx()+"&ImeMode="+params.getImeMode();		
		case 3: //범위 검색
			//
			return "PageRow="+params.getPageRow()+"&SearchPart="+params.getSearchPart()+"&SearchText="+URLEncoder.encode(params.getSearchText()[0], "UTF-8")+"&SearchStartText="+params.getSearchStartText()+"&SearchEndText="+params.getSearchEndText()
			+"&Table="+params.getTable()[0]+"&Gubun="+params.getGubun()[0]+"ResultRows="+params.getResultRow()
			+"&JasoCnt="+params.getJasoCnt()+"&JasoSearch="+params.getJasoSearch()+"&setJaso="+params.getSetJaso()
			+"&arrSearchLen="+params.getArrSearchLen()+"&go="+params.getGo()+"&gogroup="+params.getGogroup()+"&idx="+params.getIdx()+"&ImeMode="+params.getImeMode();
		case 4: //자소로 검색
			//
			return "PageRow="+params.getPageRow()+"&SearchPart="+params.getSearchPart()+"&SearchText="+URLEncoder.encode(params.getSearchText()[0], "UTF-8")+"&SearchStartText="+params.getSearchStartText()+"&SearchEndText="+params.getSearchEndText()
			+"&Table="+params.getTable()[0]+"&Gubun="+params.getGubun()[0]+"ResultRows="+params.getResultRow()
			+"&JasoCnt="+params.getJasoCnt()+"&JasoSearch="+URLEncoder.encode(params.getJasoSearch(), "UTF-8")+"&setJaso="+params.getSetJaso()
			+"&arrSearchLen="+params.getArrSearchLen()+"&go="+params.getGo()+"&gogroup="+params.getGogroup()+"&idx="+params.getIdx()+"&ImeMode="+params.getImeMode();		case 5: //따로 보기
			//
			return "SearchText="+params.getSearchText()[0]+"&Table="+params.getTable()[0]+"&TableTemp="+params.getTable()[0]+"&GubunTemp="+params.getGubun()[0]+"&Gubun="+params.getGubun()[0]+"&go="+params.getGo()+"&gogroup=&idx=&Letter="+params.getLetter();
		default :
		}
		return "";
	}
	
}
